//
// Created by 32827 on 2024/1/17.
//
#include "yzf_i2c.h"

YZF_RESULT yzf_i2c_open(YZF_I2C_POINTER i2c){
    switch (i2c) {
        case I2C0:
            rcu_periph_clock_enable(RCU_I2C0);
            break;
        case I2C1:
            rcu_periph_clock_enable(RCU_I2C1);
            break;
        case I2C2:
            rcu_periph_clock_enable(RCU_I2C2);
        default:
            return YZF_NOT_FOUND;
    }
    return YZF_OK;
}
uint32_t lastFreq;
YZF_RESULT yzf_i2c_source_clock_freq(YZF_I2C_POINTER i2c,uint32_t freq){
    i2c_clock_config(i2c,freq,I2C_DTCY_2);
    lastFreq=freq;
    return YZF_OK;
}
YZF_RESULT yzf_i2c_start(YZF_I2C_POINTER i2c){
    i2c_start_on_bus(i2c);
    return YZF_OK;
}
YZF_RESULT yzf_i2c_write_data(YZF_I2C_POINTER i2c,YZF_I2C_DATA data){
    i2c_data_transmit(i2c,data);
    return YZF_OK;
}
YZF_I2C_DATA yzf_i2c_read_data(YZF_I2C_POINTER i2c){
    return i2c_data_receive(i2c);
}
YZF_RESULT yzf_i2c_set_address_10(YZF_I2C_POINTER i2c,uint16_t address){
    i2c_mode_addr_config(i2c,I2C_I2CMODE_ENABLE,I2C_ADDFORMAT_10BITS,address);
    return YZF_OK;
}
YZF_RESULT yzf_i2c_set_address_7(YZF_I2C_POINTER i2c,uint8_t address){
    i2c_mode_addr_config(i2c,I2C_I2CMODE_ENABLE,I2C_ADDFORMAT_7BITS,address);
    return YZF_OK;
}
YZF_RESULT yzf_i2c_soft_reset(YZF_I2C_POINTER i2c){
    i2c_software_reset_config(i2c,I2C_SRESET_SET);
    return YZF_OK;
}
YZF_RESULT yzf_i2c_soft_reset_clear(YZF_I2C_POINTER i2c){
    i2c_software_reset_config(i2c,I2C_SRESET_RESET);
    return YZF_OK;
}
YZF_RESULT yzf_i2c_ack_enable(YZF_I2C_POINTER i2c){
    i2c_ack_config(i2c,I2C_ACK_ENABLE);
    return YZF_OK;
}
YZF_RESULT yzf_i2c_ack_disable(YZF_I2C_POINTER i2c){
    i2c_ack_config(i2c,I2C_ACK_DISABLE);
    return YZF_OK;
}
YZF_RESULT yzf_i2c_stop(YZF_I2C_POINTER i2c){
    i2c_stop_on_bus(i2c);
    return YZF_OK;
}
YZF_RESULT yzf_i2c_disable_clock_stretching(YZF_I2C_POINTER i2c){

    return YZF_ERROR;
}
YZF_RESULT yzf_i2c_call_enable(YZF_I2C_POINTER i2c){


    return YZF_ERROR;
}
YZF_RESULT yzf_i2c_call_disable(YZF_I2C_POINTER i2c){

    return YZF_ERROR;
}
YZF_RESULT yzf_i2c_pec_enable(YZF_I2C_POINTER i2c){
    i2c_pec_config(i2c,I2C_PEC_ENABLE);
    return YZF_OK;
}
YZF_RESULT yzf_i2c_pec_disable(YZF_I2C_POINTER i2c){
    i2c_pec_config(i2c,I2C_PEC_DISABLE);
    return YZF_OK;
}
YZF_RESULT yzf_i2c_apr_enable(YZF_I2C_POINTER i2c){
    return YZF_ERROR;
}
YZF_RESULT yzf_i2c_apr_disable(YZF_I2C_POINTER i2c){
    return YZF_ERROR;
}
YZF_RESULT yzf_i2c_enable(YZF_I2C_POINTER i2c){
    i2c_enable(i2c);
    return YZF_OK;
}
YZF_RESULT yzf_i2c_disable(YZF_I2C_POINTER i2c){
    i2c_disable(i2c);
    return YZF_OK;
}
YZF_RESULT yzf_i2c_dma_last_transfer(YZF_I2C_POINTER i2c){
    i2c_dma_last_transfer_config(i2c,I2C_DMA_ON);
    return YZF_OK;
}
YZF_RESULT yzf_i2c_dma_enable(YZF_I2C_POINTER i2c){
    i2c_dma_config(i2c,I2C_DMA_ON);
    return YZF_OK;
}
YZF_RESULT yzf_i2c_dma_disable(YZF_I2C_POINTER i2c){
    i2c_dma_config(i2c,I2C_DMA_OFF);
    return YZF_OK;
}
YZF_RESULT yzf_i2c_buffer_interrupt_enable(YZF_I2C_POINTER i2c){
    i2c_interrupt_enable(i2c,I2C_INT_BUF);
    return YZF_OK;
}
YZF_RESULT yzf_i2c_buffer_interrupt_disable(YZF_I2C_POINTER i2c){
    i2c_interrupt_disable(i2c,I2C_INT_BUF);
    return YZF_OK;
}
YZF_RESULT yzf_i2c_event_interrupt_enable(YZF_I2C_POINTER i2c){
    i2c_interrupt_enable(i2c,I2C_INT_EV);
    return YZF_OK;
}
YZF_RESULT yzf_i2c_event_interrupt_disable(YZF_I2C_POINTER i2c){
    i2c_interrupt_disable(i2c,I2C_INT_EV);
    return YZF_OK;
}
YZF_RESULT yzf_i2c_address2_enable(YZF_I2C_POINTER i2c){
    return YZF_ERROR;
}
YZF_RESULT yzf_i2c_address2_disable(YZF_I2C_POINTER i2c){
    return YZF_ERROR;
}
YZF_RESULT yzf_i2c_set_address2(YZF_I2C_POINTER i2c,uint8_t address){
    return YZF_ERROR;
}
YZF_BOOL yzf_i2c_flag_timeout(YZF_I2C_POINTER i2c){
    return i2c_flag_get(i2c,I2C_FLAG_SMBTO)==SET?1:0;
}
YZF_BOOL yzf_i2c_flag_PECERR(YZF_I2C_POINTER i2c){
    return i2c_flag_get(i2c,I2C_FLAG_PECERR)==SET?1:0;
}
YZF_BOOL yzf_i2c_flag_OVR(YZF_I2C_POINTER i2c){
    return i2c_flag_get(i2c,I2C_FLAG_OUERR)==SET?1:0;
}
YZF_BOOL yzf_i2c_flag_AF(YZF_I2C_POINTER i2c){
    return i2c_flag_get(i2c,I2C_FLAG_AERR)==SET?1:0;
}
YZF_BOOL yzf_i2c_flag_ARLO(YZF_I2C_POINTER i2c){
    return i2c_flag_get(i2c,I2C_FLAG_LOSTARB)==SET?1:0;
}
YZF_BOOL yzf_i2c_flag_BERR(YZF_I2C_POINTER i2c){
    return i2c_flag_get(i2c,I2C_FLAG_BERR)==SET?1:0;
}
YZF_BOOL yzf_i2c_flag_TxE(YZF_I2C_POINTER i2c){
    return i2c_flag_get(i2c,I2C_FLAG_TBE)==SET?1:0;
}
YZF_BOOL yzf_i2c_flag_RxNE(YZF_I2C_POINTER i2c){
    return i2c_flag_get(i2c,I2C_FLAG_RBNE)==SET?1:0;
}
YZF_BOOL yzf_i2c_flag_STOPF(YZF_I2C_POINTER i2c){
    return i2c_flag_get(i2c,I2C_FLAG_STPDET)==SET?1:0;
}
YZF_BOOL yzf_i2c_flag_add10(YZF_I2C_POINTER i2c){
    return i2c_flag_get(i2c,I2C_FLAG_ADD10SEND)==SET?1:0;
}
YZF_BOOL yzf_i2c_flag_BTF(YZF_I2C_POINTER i2c){
    return i2c_flag_get(i2c,I2C_FLAG_BTC)==SET?1:0;
}
YZF_BOOL yzf_i2c_flag_ADDR(YZF_I2C_POINTER i2c){
    return i2c_flag_get(i2c,I2C_FLAG_ADDSEND)==SET?1:0;
}
YZF_BOOL yzf_i2c_flag_SB(YZF_I2C_POINTER i2c){
    return i2c_flag_get(i2c,I2C_FLAG_SBSEND)==SET?1:0;
}
YZF_I2C_PEC_DATA yzf_i2c_get_pec(YZF_I2C_POINTER i2c){
    return i2c_flag_get(i2c,I2C_FLAG_PECERR);
}
YZF_BOOL yzf_i2c_flag_DUALF(YZF_I2C_POINTER i2c){
    return i2c_flag_get(i2c,I2C_FLAG_DUMOD)==SET?1:0;
}
YZF_BOOL yzf_i2c_flag_GENCALL(YZF_I2C_POINTER i2c){
    return i2c_flag_get(i2c,I2C_FLAG_RXGC)==SET?1:0;
}
YZF_BOOL yzf_i2c_flag_TRA(YZF_I2C_POINTER i2c){
    i2c_flag_get(i2c,I2C_FLAG_TR);
    return YZF_FALSE;
}
YZF_BOOL yzf_i2c_flag_BUSY(YZF_I2C_POINTER i2c){
    return i2c_flag_get(i2c,I2C_FLAG_I2CBSY)==SET?1:0;
}
YZF_BOOL yzf_i2c_flag_MSL(YZF_I2C_POINTER i2c){
    return i2c_flag_get(i2c,I2C_FLAG_MASTER)==SET?1:0;
}
YZF_RESULT yzf_i2c_set_high_mode(YZF_I2C_POINTER i2c){
    return YZF_ERROR;
}
YZF_RESULT yzf_i2c_set_normal_mode(YZF_I2C_POINTER i2c){
    return YZF_ERROR;
}
YZF_RESULT yzf_i2c_duty_cycle_2(YZF_I2C_POINTER i2c){
    i2c_clock_config(i2c,lastFreq,I2C_DTCY_2);
    return YZF_OK;
}
YZF_RESULT yzf_i2c_duty_cycle_16_9(YZF_I2C_POINTER i2c){
    i2c_clock_config(i2c,lastFreq,I2C_DTCY_16_9);
    return YZF_OK;
}
YZF_RESULT yzf_i2c_set_CCR(YZF_I2C_POINTER i2c,uint16_t ccr){
    return YZF_ERROR;
}
YZF_RESULT yzf_i2c_trise(YZF_I2C_POINTER i2c,uint16_t trise){
    return YZF_ERROR;
}